<?php
class penjualancontroller extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper('form');
		$this->load->helper('currupiah');
		$this->load->library('form_validation');
		$this->load->library('session');
		$this->load->library('pagination_editable');
		//$this->load->library('cart');
		$this->load->model('customstock_model');		
		if(($this->session->userdata('fullname') == '') || ($this->session->userdata('fullname') == NULL)){
			redirect('logincontroller');
		}
	}
	
	function index(){		
		$data = array(
			'data_array' => $this->customstock_model->getItemCategory(),
			'page_title' => 'Finance > Transaksi Penjualan',
			'name' => $this->session->userdata('fullname')
		);
		$content = array('content' => 'sales/penjualan/categoryview');
		$this->template->load('templates/default',$content,$data);
	}
	
	function stockList(){		
		$id = $this->uri->segment(3);
		$this->customstock_model->setKodeBarang($id);
				
		$data = array(
			'array_stock' => $this->customstock_model->getStockList(),
			'page_title' => 'Finance > Transaksi Penjualan',
			'name' => $this->session->userdata('fullname')
		);
		$content = array('content' => 'sales/penjualan/stocklist');
		$this->template->load('templates/default',$content,$data);
	}
	
	function addtocart(){
		$array_items = array();
		$cart_array = array();
		$i = 0;
		while($i < count($_POST['chkitem'])){			
			$this->customstock_model->setKodeBarang($_POST['chkitem'][$i]);			
			$dataarray = $this->customstock_model->getItemsContent();
			$qty = $_POST['txtQty'][$i];
			$price = $_POST['txtPrice'][$i];
			$disc = $_POST['txtDisc'][$i];
			if(count($_POST['chkitem']) < 2){
				foreach($this->input->post('txtQty') as $itemQty){
					if($itemQty != ""){
						$qty = $itemQty;
					}
				}
				foreach($this->input->post('txtPrice') as $itemPrice){
					if($itemPrice != ""){
						$price = $itemPrice;
					}
				}
				foreach($this->input->post('txtDisc') as $itemDisc){
					if($itemDisc != ""){
						$disc = $itemDisc;
					}
				}				
			}
			 
			$array_items = array(
				 'id'   => $dataarray[0]->kodedetailbarang,
                 'qty'  => $qty,
               	 'price'=> $price,
                 'name' => $dataarray[0]->productname,
				 'options' => array(
				 	'typeunit' => $dataarray[0]->typeunit,
					'kodesupplier' => $dataarray[0]->kodesupplier,
					'suppliername' => $dataarray[0]->suppliername,
					'disc' => $disc
				 )
			);
			$cart_array[$i] = $array_items;						
			$i++;			
		}		
		$this->cart->insert($cart_array);
		redirect('penjualancontroller/showFormPenjualan');
	}
	
	function showFormPenjualan(){		
		$fakturNo = $this->customstock_model->getFakturAutoNo();
		$data_array = $this->session->userdata('cart');		
		$data = array(
			'data_post' => $this->input->post(),			
			'page_title' => 'Finance > Cart',
			'name' => $this->session->userdata('fullname'),
			'latest_faktur' => $fakturNo,
			'bankcombo' => $this->bankCombobox(),
			'mydatas' => $data_array						
		);
		$content = array('content' => 'sales/penjualan/formpenjualan');
		$this->template->load('templates/default',$content,$data);
	}
	
	function bankCombobox(){
		$this->load->model('masterbank_model');
		$data = $this->masterbank_model->getListbank();
		$mycombo = '';
		$mycombo = '<select id="bank" name="bank" onchange="javascript:getBankDetail()">';
		$mycombo .= '<option value="0">Pilih Bank</option>';
		foreach($data as $bank){			
			$mycombo .= '<option value="'.$bank->KodeBank.'">'.$bank->Bankname.'</option>';
		}
		$mycombo .= '</select>';
		return $mycombo;
	}
	
	function getBankDetail(){
		$this->load->model('masterbank_model');
		$bankid = $_POST['bankid'];
		$this->masterbank_model->setKodeBank($bankid);
		$resdata = $this->masterbank_model->getBankPerRecord();
		print json_encode($resdata[0]);
	}
	
	function emptyCart(){
		$this->cart->destroy();
		redirect('penjualancontroller');
	}
	
	function saveFakturPenjualan(){
		$this->load->model('transfakprodjual_model');
		$this->load->model('lappersediaandetail_model');
		$this->load->model('stock_model');
		$this->load->model('kwitansipenjualan_model');
		$this->load->model('mutasibank_model');
		$this->load->model('fakturproductpenjualan_model');
		$this->load->model('transactionkwitansi_model');
		$this->load->model('subfaktur_model');
		
		$this->load->model('kwitansimutasicash_model');
		$this->load->model('kwitansimutasibank_model');
		$this->load->model('kwitansimutasigiro_model');
		
		$i = 0;
		$sb = 0;
		$ttlqty		 = 0;
		$ttlamtout=0;
		$ttlamt	 = 0;
		$ttlamtall	 = 0;
		$noKwitansi  = "";
		$stockstatus="JUAL";

		$tglfaktur =date('Y-m-d', strtotime('now'));
		$tanggal=date('Y-m-d H:i:s', strtotime('now'));
		
		$noKwitansi = $this->kwitansipenjualan_model->getkwitansiCode();
		$this->kwitansipenjualan_model->setNoKwitansi($noKwitansi);
				
		//print_r($_POST);exit;
		foreach($this->input->post('txtkdbrg') as $items)
		{	
			// insert ke tabel transaksi faktur product penjualan			
			$this->transfakprodjual_model->setNoFaktur($this->input->post('nofaktur'));
			$this->transfakprodjual_model->setKodeBarang($_POST['txtkdbrg'][$i]);
			$this->transfakprodjual_model->setHargaBarang($_POST['txtitemprice'][$i]);
			$this->transfakprodjual_model->setQuantity(intval($_POST['txtqty'][$i]));
			$this->transfakprodjual_model->setDiscount($_POST['txtdisc'][$i]);
			$this->transfakprodjual_model->setPpn(strval('0'));
			$this->transfakprodjual_model->setAmount($_POST['txtTotal'][$i]);			
			$this->transfakprodjual_model->insert();
			
			// insert ke tabel stock dan transaksi stock
			$this->stock_model->setKodeSupplier($_POST['txtkodesupplier'][$i]);
			$this->stock_model->setKodeDetailBarang($_POST['txtkdbrg'][$i]);
			$stockArray['stok'] = $this->stock_model->cekkodestck();
			$stockkode = $this->stock_model->getStockListId();
			$stockAkhir = $this->stock_model->getStockCurrent();

			if($stockArray['stok'] != "0")
			{
										
				$this->stock_model->setKodeStok($stockkode[0]->kodestok);
				$latest_stock = intval($stockAkhir[0]->stockakhir) + intval($_POST['txtqty'][$i]);
				$this->stock_model->setStokAkhir($latest_stock);
				$this->stock_model->setLastUpdateBy($this->session->userdata('userid'));
				$this->stock_model->setLastUpdateOn(date('Y-m-d'));
				$this->stock_model->update();
				
				$this->stock_model->setKodeStok($stockkode[0]->kodestok);
				$this->stock_model->setQuantity($_POST['txtqty'][$i]);
				$this->stock_model->setStatus($stockstatus);
				$this->stock_model->setCreatedBy($this->session->userdata('userid'));
				$this->stock_model->setCreatedOn($tanggal);
				$this->stock_model->insertdetail();
			}
			else
			{
										
				$codestock = $this->stock_model->ambilkodestock();
				$this->stock_model->setKodeStok($codestock);
				$this->stock_model->setKodeSupplier($_POST['txtkodesupplier'][$i]);
				$this->stock_model->setKodeDetailBarang($_POST['txtkdbrg'][$i]);
				$this->stock_model->setStokAkhir($_POST['txtqty'][$i]);
				$this->stock_model->setCreatedBy($this->session->userdata('userid'));
				$this->stock_model->setCreatedOn($tanggal);
				$this->stock_model->insert();	
				
				$this->stock_model->setKodeStok($codestock);
				$this->stock_model->setQuantity($_POST['txtqty'][$i]);
				$this->stock_model->setStatus($stockstatus);
				$this->stock_model->setCreatedBy($this->session->userdata('userid'));
				$this->stock_model->setCreatedOn($tanggal);
				$this->stock_model->insertdetail();
			}
			
			$ttlqty		 = $ttlqty + $_POST['txtqty'][$i];
			$ttlamt		 = $ttlamt + $_POST['ttlamount'][$i];
			
			// insert ke tabel transaksi kwitansi
			$this->transactionkwitansi_model->setNoKwitansi($noKwitansi);
			$this->transactionkwitansi_model->setNoFaktur($this->input->post('nofaktur'));
			$this->transactionkwitansi_model->setQuantity($_POST['txtqty'][$i]);
			$this->transactionkwitansi_model->setDiscount($_POST['txtdisc'][$i]);
			$this->transactionkwitansi_model->setPPN(strval('0'));
			$this->transactionkwitansi_model->setAmount($_POST['ttlamount'][$i]);
			$this->transactionkwitansi_model->insert();

			$i++;
		}
		
		$nosubfaktur = $this->subfaktur_model->getkodenofak();
		$this->subfaktur_model->setNoSubFaktur($nosubfaktur);
		
		$typeBayar = "";
		switch($_POST['payment'])
		{
			case "1" : $typeBayar = "CASH";break;
			case "2" : $typeBayar = "GIRO";break;
			case "3" : $typeBayar = "BANK";break;
		}
		
		if ($typeBayar=="CASH")
		{	
			// insert ke tabel Cash
			$this->kwitansimutasicash_model->setNoKwitansi($noKwitansi);
			$this->kwitansimutasicash_model->setNoFaktur($this->input->post('nofaktur'));
			$this->kwitansimutasicash_model->setTotalQuantity($ttlqty);
			$this->kwitansimutasicash_model->setDiscount(strval('0'));
			$this->kwitansimutasicash_model->setTotalAmount_In($_POST['ttlbayar']);
			$this->kwitansimutasicash_model->setTotalAmount_Out($ttlamtout);
			$this->kwitansimutasicash_model->setKeterangan($_POST['keterangan']);
			$this->kwitansimutasicash_model->setCreatedBy($this->session->userdata('userid'));
			$this->kwitansimutasicash_model->setCreatedOn($tanggal);
			$this->kwitansimutasicash_model->setActive('1');
			$this->kwitansimutasicash_model->insert();
			
			// insert ke tabel sub faktur
			if($_POST['ttlbayar'] != $ttlamt)
			{	
				$ttlamtall = $ttlamt - intval($_POST['ttlbayar']);				
				$this->subfaktur_model->setNoFaktur($this->input->post('nofaktur'));
				$this->subfaktur_model->setNamacustomer($_POST['taNamaPelanggan']);
				$this->subfaktur_model->setDatesubfaktur(date('Y-m-d'));
				$this->subfaktur_model->setQuantity($ttlqty);
				$this->subfaktur_model->setAmount($ttlamtall);
				$this->subfaktur_model->setStatus("Belum Lunas");
				$this->subfaktur_model->setKeterangan($_POST['keterangan']);
				$this->subfaktur_model->setCreatedBy($this->session->userdata('userid'));
				$this->subfaktur_model->setCreatedOn($tanggal);
				$this->subfaktur_model->insert();
			}
		}
		elseif ($typeBayar=="BANK")
		{
			// insert ke tabel mutasi bank
			$this->kwitansimutasibank_model->setNoKwitansi($noKwitansi);
			$this->kwitansimutasibank_model->setNoFaktur($this->input->post('nofaktur'));
			$this->kwitansimutasibank_model->setTotalQuantity($ttlqty);
			$this->kwitansimutasibank_model->setDiscount(strval('0'));
			$this->kwitansimutasibank_model->setTotalAmount_In($_POST['ttlamountbank']);
			$this->kwitansimutasibank_model->setTotalAmount_Out($ttlamtout);
			$this->kwitansimutasibank_model->setKodeBank($_POST['bank']);
			$this->kwitansimutasibank_model->setKeterangan($_POST['keterangan']);
			$this->kwitansimutasibank_model->setCreatedBy($this->session->userdata('userid'));
			$this->kwitansimutasibank_model->setCreatedOn($tanggal);
			$this->kwitansimutasibank_model->setActive('1');
			$this->kwitansimutasibank_model->insert();
			
			// insert ke tabel sub faktur
			if($_POST['ttlamountbank'] != $ttlamt)
			{	
				$ttlamtall = $ttlamt - $_POST['ttlamountbank'];
				$this->subfaktur_model->setNoFaktur($this->input->post('nofaktur'));
				$this->subfaktur_model->setDatesubfaktur(date('Y-m-d'));
				$this->subfaktur_model->setNamacustomer($_POST['taNamaPelanggan']);
				$this->subfaktur_model->setQuantity($ttlqty);
				$this->subfaktur_model->setAmount($ttlamtall);
				$this->subfaktur_model->setStatus("Belum Lunas");
				$this->subfaktur_model->setKeterangan($_POST['keterangan']);
				$this->subfaktur_model->setCreatedBy($this->session->userdata('userid'));
				$this->subfaktur_model->setCreatedOn($tanggal);
				$this->subfaktur_model->insert();
			}
		}
		elseif ($typeBayar=="GIRO")
		{
			// insert ke tabel mutasi Giro
			
			$this->kwitansimutasigiro_model->setNoKwitansi($noKwitansi);
			$this->kwitansimutasigiro_model->setNoFaktur($this->input->post('nofaktur'));
			$this->kwitansimutasigiro_model->setTotalQuantity($ttlqty);
			$this->kwitansimutasigiro_model->setTotalAmount_In($_POST['ttlamountgiro']);
			$this->kwitansimutasigiro_model->setTotalAmount_Out($ttlamtout);
			$this->kwitansimutasigiro_model->setBankname($_POST['nmbankgiro']);
			$this->kwitansimutasigiro_model->setNogiro($_POST['nogiro']);
			$this->kwitansimutasigiro_model->setTanggalterbit($_POST['tglterbit']);
			$this->kwitansimutasigiro_model->setTanggalcair($_POST['tglcair']);
			$this->kwitansimutasigiro_model->setKeterangan($_POST['keterangan']);
			$this->kwitansimutasigiro_model->setCreatedBy($this->session->userdata('userid'));
			$this->kwitansimutasigiro_model->setCreatedOn($tanggal);
			$this->kwitansimutasigiro_model->setActive('1');
			$this->kwitansimutasigiro_model->insert();
			
			// insert ke tabel sub faktur
			if($_POST['ttlamountgiro'] != $ttlamt)
			{	
				$ttlamtall = $ttlamt - $_POST['ttlamountgiro'];
				$this->subfaktur_model->setNoFaktur($this->input->post('nofaktur'));
				$this->subfaktur_model->setDatesubfaktur(date('Y-m-d'));
				$this->subfaktur_model->setNamacustomer($_POST['taNamaPelanggan']);
				$this->subfaktur_model->setQuantity($ttlqty);
				$this->subfaktur_model->setAmount($ttlamtall);
				$this->subfaktur_model->setStatus("Belum Lunas");
				$this->subfaktur_model->setKeterangan($_POST['keterangan']);
				$this->subfaktur_model->setCreatedBy($this->session->userdata('userid'));
				$this->subfaktur_model->setCreatedOn($tanggal);
				$this->subfaktur_model->insert();
			}
		}
				
		// insert ke tabel kwitansi penjualan
		$this->kwitansipenjualan_model->setNoFaktur($this->input->post('nofaktur'));
		$this->kwitansipenjualan_model->setnamapelanggan($_POST['taNamaPelanggan']);
		//$this->kwitansipenjualan_model->setKodeBarang($_POST['txtkdbrg'][$i]);
		$this->kwitansipenjualan_model->setTotalQuantity($ttlqty);
		//$this->kwitansipenjualan_model->setDiscount($_POST['txtdisc'][$i]);
		$this->kwitansipenjualan_model->setTotalAmount($ttlamt);
		
		
		$this->kwitansipenjualan_model->setTypebayar($typeBayar);
		
		if ($typeBayar=="BANK")
		{
			$this->kwitansipenjualan_model->setKodeBank($_POST['bank']);
			$this->kwitansipenjualan_model->setBanknamepelanggan($_POST['nmbankpembeli']);
			$this->kwitansipenjualan_model->setNoRekeningpelanggan($_POST['noreknorekbuyer']);
		}
		elseif ($typeBayar=="GIRO")
		{
			$this->kwitansipenjualan_model->setKodeBank("-");
			$this->kwitansipenjualan_model->setBanknamepelanggan($_POST['nmbankgiro']);
			$this->kwitansipenjualan_model->setNoRekeningpelanggan($_POST['nogiro']);
		}
		else
		{
			$this->kwitansipenjualan_model->setKodeBank("-");
			$this->kwitansipenjualan_model->setBanknamepelanggan("-");
			$this->kwitansipenjualan_model->setNoRekeningpelanggan("0");	
		}
		$this->kwitansipenjualan_model->setKeterangan($_POST['keterangan']);
		$this->kwitansipenjualan_model->setCreatedBy($this->session->userdata('userid'));
		$this->kwitansipenjualan_model->setCreatedOn();
		$this->kwitansipenjualan_model->setLastupdateby($this->session->userdata('userid'));
		$this->kwitansipenjualan_model->setLastupdateon($tanggal);
		$this->kwitansipenjualan_model->setActive(strval('1'));
		$this->kwitansipenjualan_model->insert();
		
		// insert ke tabel faktur product penjualan
		$this->fakturproductpenjualan_model->setNoFaktur($this->input->post('nofaktur'));
		$this->fakturproductpenjualan_model->setDateFaktur(date('Y-m-d'));
		$this->fakturproductpenjualan_model->setTotalQuantity($ttlqty);
		$this->fakturproductpenjualan_model->setTotalAmount($ttlamt);
		$this->fakturproductpenjualan_model->setTypeBayar($_POST['payment']);
		
		if($_POST['payment'] == "2"){
			$this->fakturproductpenjualan_model->setBankName($_POST['nmbankgiro']);
		}else if($_POST['payment'] == "3"){
			$this->fakturproductpenjualan_model->setBankName($_POST['bank']);
		}
		
		$this->fakturproductpenjualan_model->setKeterangan($_POST['keterangan']);
		$this->fakturproductpenjualan_model->setCreatedBy($this->session->userdata('userid'));
		$this->fakturproductpenjualan_model->setCreatedOn(date('Y-m-d'));
		$this->fakturproductpenjualan_model->setActive('1');			
		$this->fakturproductpenjualan_model->insert();
		
		$stockamtsubfak['amtsubfak'] = $this->subfaktur_model->cekamountsubfak();
		
		$this->cart->destroy();
		$dataprofile = array(
			'page_title' => 'Sales > Kwitansi Penjualan',
			'name' => $this->session->userdata('fullname'),
			'kodekwitansino'=> $this->kwitansipenjualan_model->ambilnokwitansi1(),
			'kodecustkwitansi'=> $this->kwitansipenjualan_model->ambilnokwitansi1(),
			'ambilamountsubfaktur'=> $stockamtsubfak['amtsubfak'],
			//'ambilamountsubfaktur'=> $this->subfaktur_model->ambilamountsubfak(),
			'transkwitansi' => $this->kwitansipenjualan_model->getListfakturkwt()	
		);		
		$content = array(
			'content' => 'sales/penjualan/formkwitansipenjual'
		);
		$this->template->load('templates/default',$content,$dataprofile);		
		
		//$this->emptyCart();		
		
		//redirect('penjualancontroller');
	}
}